package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.commons.template.OssTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.sound.Sound;
import com.tanhua.domain.sound.SoundCount;
import com.tanhua.domain.sound.SoundCountVo;
import com.tanhua.dubbo.api.SoundApi;
import com.tanhua.dubbo.api.SoundCountApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author To my left is the dog
 * @date 2021/2/28 15:49
 */
@Service
public class SoundService {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Reference
    private SoundApi soundApi;
    @Autowired
    private OssTemplate ossTemplate;
    @Reference
    private SoundCountApi soundCountApi;
    @Reference
    private UserInfoApi userInfoApi;

    //发送语音 保存语音
    public ResponseEntity saveSound(MultipartFile soundFile) throws IOException {
        /*//语音审核
        //上传到阿里云存储
        String soundPath = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());*/
        String soundName = soundFile.getOriginalFilename();
        String soundSuffix = soundName.substring(soundName.lastIndexOf(".") + 1);
        StorePath storePath = fastFileStorageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), soundSuffix, null);
        String soundPath = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();

        //根据id查找它的次数表
        SoundCount soundCount = soundCountApi.findById(UserHolder.getUserId());
        //如果是新用户，新增发送语音次数表
        if (soundCount == null) {
            soundCount = new SoundCount();
            soundCount.setId(new ObjectId());
            soundCount.setUserId(UserHolder.getUserId());
            soundCount.setRemainingTimes(10);
            soundCount.setCreated(System.currentTimeMillis());
            soundCountApi.save(soundCount);
        }
        //保存当前用户语音表
        Sound sound = new Sound();
        sound.setSoundUrl(soundPath);
        sound.setUserId(UserHolder.getUserId());
        soundApi.saveSound(sound);
        return ResponseEntity.ok(null);
    }

    /*接收语音
     *
     *  @return
     *  返回一个在区间0 - Long.MAX_VALUE的long类型的数
     *  long lontt = RandomUtils.nextLong();
     */
    public ResponseEntity acceptSound() {
        //获取到当前用户id
        Long userId = UserHolder.getUserId();
        //获取随机id
        Long userIdRandom = null;
        //随机获取id 范围为 1到10 暂时写死
        while (true) {
            userIdRandom = RandomUtils.nextLong(1, 10);
            if (userId != userIdRandom) {
                break;
            }
        }
        //根据随机来的id查找语音
        Sound sound = soundApi.findById(userIdRandom);
        if (sound == null) {
            //如果随机到的id 没有查询到 给个默认值
            Long random = Long.valueOf(1);
            sound = soundApi.findById(random);
        }

        //根据当前用户id获取到它的剩余次数
        SoundCount soundCount = soundCountApi.findById(userId);
        if (soundCount == null) {
            soundCount = new SoundCount();
            soundCount.setId(new ObjectId());
            soundCount.setUserId(UserHolder.getUserId());
            soundCount.setRemainingTimes(10);
            soundCount.setCreated(System.currentTimeMillis());
            soundCountApi.save(soundCount);
        }
        //每接收一次语音 对当前用户的剩余次数减1
        soundCountApi.update(userId);

        //封装数据
        SoundCountVo vo = new SoundCountVo();
        //封装 用户id 头像 昵称 性别 年龄
        UserInfo userInfo = userInfoApi.findById(userIdRandom);
        vo.setId(userInfo.getId().intValue());
        vo.setAvatar(userInfo.getAvatar());
        vo.setNickname(userInfo.getNickname());
        vo.setGender(userInfo.getGender());
        vo.setAge(userInfo.getAge());

        //封装 语音地址 剩余次数
        vo.setSoundUrl(sound.getSoundUrl());
        vo.setRemainingTimes(soundCount.getRemainingTimes());

        soundApi.remove(userIdRandom);

        return ResponseEntity.ok(vo);
    }
}
